package com.project.frba.bi.user;

import java.util.List;

import org.springframework.jdbc.core.RowMapperResultSetExtractor;

import com.project.frba.bi.queueing.AbstractDAO;
import com.project.frba.bifront.client.model.User;

public class UserDAO extends AbstractDAO{
	
	public User getUserByUsername(String username){
		String query = UserQueryBuilder.getUserByUsernameQuery();
		Object[] params = {username};
		return (User)jdbcTemplate.queryForObject(query, params, new UserRowMapper());
	}
	
	public User getUserById(Long id){
		String query = UserQueryBuilder.getUserByIdQuery();
		Object[] params = {id};
		return (User)jdbcTemplate.queryForObject(query, params, new UserRowMapper());
	}
	
	public void removeUser(Long id){
		String query = UserQueryBuilder.removeUserQuery();
		Object[] params = {id};
		jdbcTemplate.update(query, params);
	}

	public List<User> getAllUsers() {
		String query = UserQueryBuilder.getAllUsersQuery();
		RowMapperResultSetExtractor usersExtractor = new RowMapperResultSetExtractor(new UserRowMapper());
		return (List<User>)jdbcTemplate.query(query, usersExtractor);
	}

	public Integer insertUser(String username, String password, Long privilege) {
		String query = UserQueryBuilder.insertUserQuery();
		Object[] params = {
				username,
				password,
				privilege
		};
		return jdbcTemplate.update(query, params);
	}
	
	public void updateUser(Long id, String username, String password, Long privilege){
		String query = UserQueryBuilder.updateUserQuery();
		Object[] params = {
				username,
				password,
				privilege,
				id
		};
		jdbcTemplate.update(query, params);
	}

}
